package com.warmlight.voicepacket.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.umeng.analytics.MobclickAgent;
import com.warmlight.voicepacket.MyPlaylistEditActivity;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.base.MBaseAdapter;
import com.warmlight.voicepacket.dbdata.DBFloatPlayListDetail;
import com.warmlight.voicepacket.dbdata.DBFloatPlaylist;
import com.warmlight.voicepacket.dbmanager.DBFloatPlayListDetailDaoHelp;
import com.warmlight.voicepacket.utils.MobClickHelper;
import com.zhuge.analysis.stat.ZhugeSDK;

import java.util.HashMap;
import java.util.List;

import static com.warmlight.voicepacket.db.VoicePacketDBHelper.LIST_ID;

/**
 * 未登录时的我的播单列表
 * Created by jason on 2018/6/4.
 */

public class MyPlaylistAdapter extends MBaseAdapter {

    private DBFloatPlaylist mTemp;

    public MyPlaylistAdapter(Context context, List<DBFloatPlaylist> list) {
        super(context, list);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(mContext, R.layout.item_myplaylist, null);
            holder = new ViewHolder();
            holder.tv_myplaylist_title = (TextView) convertView.findViewById(R.id.tv_myplaylist_title);
            holder.tv_myplaylist_count = (TextView) convertView.findViewById(R.id.tv_myplaylist_count);
            holder.iv_myplaylist_edit = (ImageView) convertView.findViewById(R.id.iv_myplaylist_edit);
            holder.iv_myplaylist = (ImageView) convertView.findViewById(R.id.iv_myplaylist);
            holder.iv_cover = (ImageView) convertView.findViewById(R.id.iv_cover);
            convertView.setTag(holder);
        }

        DBFloatPlaylist data = (DBFloatPlaylist)mList.get(position);
        holder.tv_myplaylist_title.setText(data.getList_name());
        holder.tv_myplaylist_count.setText(data.getList_count()+"个");
        if ("我的最爱".equals(data.getList_name())){
            holder.iv_cover.setVisibility(View.VISIBLE);
        }else{
            holder.iv_cover.setVisibility(View.GONE);
        }
        RequestOptions options = new RequestOptions();
        options.dontAnimate();
        if("0".equals(data.getList_count())){
            Glide.with(mContext).asBitmap().load(R.drawable.icon_iv_fail).apply(options).into(holder.iv_myplaylist);
        }else{
//            List<DBFloatPlayListDetail> listAudios = DBHelper.getInstance(mContext).queryAudiosByListId(data.get_id());
            List<DBFloatPlayListDetail> listAudios =  DBFloatPlayListDetailDaoHelp.queryBuilder(mContext,data.getId()+"");
            if(listAudios!=null && listAudios.size()>0){
                String url = listAudios.get(0).getAudio_ps();
                Glide.with(mContext).asBitmap().load(TextUtils.isEmpty(url) ? R.drawable.icon_iv_fail : url).apply(options).into(holder.iv_myplaylist);
            }else {
                Glide.with(mContext).asBitmap().load(R.drawable.icon_iv_fail).apply(options).into(holder.iv_myplaylist);
            }

        }

        //如果播单名字是"我的最爱"则隐藏"更多"点点点按钮
        if("我的最爱".equals(data.getList_name().trim())){
            holder.iv_myplaylist_edit.setVisibility(View.INVISIBLE);
        }else{
            holder.iv_myplaylist_edit.setVisibility(View.VISIBLE);
        }

        holder.iv_myplaylist_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTemp = (DBFloatPlaylist) mList.get(position);
                Intent intent = new Intent(mContext, MyPlaylistEditActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(LIST_ID,(DBFloatPlaylist)mList.get(position));
                intent.putExtras(bundle);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //我的播单条目更多
                MobclickAgent.onEvent(mContext.getApplicationContext(), MobClickHelper.CLOT_FEED_MORE,((DBFloatPlaylist) mList.get(position)).getList_name());
                HashMap<String,Object> temp = new HashMap<>();
                temp.put("我的播单条目更多",((DBFloatPlaylist) mList.get(position)).getList_name().toString());
                ZhugeSDK.getInstance().track(mContext.getApplicationContext(),MobClickHelper.CLOT_FEED_MORE,temp);

                mContext.startActivity(intent);

            }
        });

        return convertView;
    }

    private class ViewHolder {
        ImageView iv_myplaylist;
        ImageView iv_myplaylist_edit;
        TextView tv_myplaylist_title;
        TextView tv_myplaylist_count;
        ImageView iv_cover;
    }
}
